package org.example.springboot01.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.springboot01.mapper.Articlemapper;
import org.example.springboot01.pojo.Article;
import org.example.springboot01.pojo.PageBean;
import org.example.springboot01.service.Articleservice;
import org.example.springboot01.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleserviceImpl implements Articleservice {
    @Autowired
    private Articlemapper articlemapper;


    @Override
    public void addarticle(Article article) {
//        实现其他参数的赋值
        article.setCreate_time(LocalDateTime.now().toString());
        article.setUpdate_time(LocalDateTime.now().toString());
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        article.setCreater_user(username);
        articlemapper.addarticle(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //生成pagebean对象
        PageBean<Article> pageBean = new PageBean<>();
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //sql
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");  System.out.println("username = " + username);
        //正常输出  username
        List<Article> list = articlemapper.list(username,categoryId, state);
        //把查到的放到list
        Page<Article> page = (Page<Article>) list;
        //把总条数放到pagebean
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }
    //根据id查询文章
    @Override
    public Article getarticle(Integer id) {
       return articlemapper.getarticle(id);

    }

    @Override
    public void updatearticle(Article article) {
        article.setUpdate_time(LocalDateTime.now().toString());
        Integer id = article.getId();
        articlemapper.updatearticle(article,id);
    }

    @Override
    public void deletearticle(Integer id) {
        articlemapper.deletearticle(id);
    }


}

